package com.iedu.demo.lgq.controller;

import com.iedu.demo.lgq.entity.ChatMessage;
import com.iedu.demo.lgq.service.ChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/chat")
public class ChatController {

    @Autowired
    private ChatService chatService;

    // 发送消息
    @PostMapping("/send")
    public void sendMessage(@RequestParam String sender,
                            @RequestParam String receiver,
                            @RequestParam String content) {
        ChatMessage chatMessage = new ChatMessage();
        chatMessage.setSender(sender);
        chatMessage.setReceiver(receiver);
        chatMessage.setContent(content);
        chatService.sendMessage(chatMessage);
    }

    // 获取聊天记录
    @GetMapping("/history")
    public List<ChatMessage> getChatHistory(@RequestParam String sender,
                                            @RequestParam String receiver) {
        return chatService.getChatHistory(sender, receiver);
    }

    // 获取未读消息
    @GetMapping("/unread")
    public List<ChatMessage> getUnreadMessages(@RequestParam String receiver,
                                               @RequestParam Long lastTimestamp) {
        Date lastTime = new Date(lastTimestamp);
        return chatService.getUnreadMessages(receiver, lastTime);
    }
}